// 下载保存模块

let savaMediaPermission = false; // 保存权限
// 下载保存图片
export function savaImage(url, callback = () => {}) {
    if (url) {
        // #ifdef MP-WEIXIN
        // 小程序平台
        if (savaMediaPermission) {
            // 获取权限设置
            uni.getSetting({
                success: setting => {
                    // 判断保存相册权限
                    if (setting.authSetting['scope.writePhotosAlbum']) {
                        // 执行下载
                        downloadFile(url, filePath => {
                            uni.saveImageToPhotosAlbum({
                                filePath,
                                success: () => {
                                    uni.showToast({
                                        title: '保存成功'
                                    });
                                    callback()
                                },
                                fail: (err) => {
                                    uni.showToast({
                                        title: "保存失败,错误原因：" + err.errMsg,
                                        icon: "none"
                                    });
                                }
                            });
                        });
                    } else {
                        uni.showModal({
                            title: '提示',
                            content: '请先在设置页面打开“保存相册”使用权限',
                            confirmText: '去设置',
                            cancelText: '算了',
                            success: data => {
                                if (data.confirm) {
                                    uni.openSetting();
                                }
                            }
                        });
                    }
                }
            });
        } else {
            savaMediaPermission = true;
            uni.authorize({
                scope: 'scope.writePhotosAlbum',
                success: () => {
                    // 执行下载
                    downloadFile(url, filePath => {
                        uni.saveImageToPhotosAlbum({
                            filePath,
                            success: () => {
                                uni.showToast({
                                    title: '保存成功'
                                });
                                callback()
                            },
                            fail: (err) => {
                                uni.showToast({
                                    title: "保存失败,错误原因：" + err.errMsg,
                                    icon: "none"
                                });
                            }
                        });
                    });
                }
            });
        }
        // #endif

        // #ifdef H5
        // H5平台
        // 执行下载
        downloadFile(url, filePath => {
            window.open(filePath);
            callback();
        });
        // #endif

        // #ifdef APP-PLUS
        // APP平台
        uni.showLoading({
            title: '正在下载...'
        });
        uni.saveImageToPhotosAlbum({
            filePath: url,
            success: () => {
                uni.hideLoading();
                uni.showToast({
                    title: '保存成功'
                });
                callback();
            },
            fail: (err) => {
                uni.hideLoading();
                uni.showToast({
                    title: '保存失败，错误原因：' + err.errMsg,
                    icon: "none"
                });
            }
        });
        // #endif
    } else {
        uni.showToast({
            title: "url参数未设置",
            icon: "none"
        })
    }
};

// 下载保存视频
export function savaVideo(url, callback = () => {}) {
    if (url) {
        // #ifdef MP-WEIXIN
        // 小程序平台
        if (savaMediaPermission) {
            // 获取权限设置
            uni.getSetting({
                success: setting => {
                    // 判断保存相册权限
                    if (setting.authSetting['scope.writePhotosAlbum']) {
                        // 执行下载
                        downloadFile(url, filePath => {
                            uni.saveImageToPhotosAlbum({
                                filePath,
                                success: () => {
                                    uni.showToast({
                                        title: '保存成功'
                                    });
                                    callback()
                                },
                                fail: (err) => {
                                    uni.showToast({
                                        title: "保存失败,错误原因：" + err.errMsg,
                                        icon: "none"
                                    });
                                }
                            });
                        });
                    } else {
                        uni.showModal({
                            title: '提示',
                            content: '请先在设置页面打开“保存相册”使用权限',
                            confirmText: '去设置',
                            cancelText: '算了',
                            success: data => {
                                if (data.confirm) {
                                    uni.openSetting();
                                }
                            }
                        });
                    }
                }
            });
        } else {
            savaMediaPermission = true;
            uni.authorize({
                scope: 'scope.writePhotosAlbum',
                success: () => {
                    // 执行下载
                    downloadFile(url, filePath => {
                        uni.saveImageToPhotosAlbum({
                            filePath,
                            success: () => {
                                uni.showToast({
                                    title: '保存成功'
                                });
                                callback()
                            },
                            fail: (err) => {
                                uni.showToast({
                                    title: "保存失败,错误原因：" + err.errMsg,
                                    icon: "none"
                                });
                            }
                        });
                    });
                }
            });
        }
        // #endif

        // #ifdef H5
        // H5平台
        // 执行下载
        downloadFile(url, filePath => {
            window.open(filePath);
            callback();
        });
        // #endif

        // #ifdef APP-PLUS
        // APP平台
        uni.showLoading({
            title: '正在下载...'
        });
        uni.saveVideoToPhotosAlbum({
            filePath: url,
            success: () => {
                uni.hideLoading();
                uni.showToast({
                    title: '保存成功'
                });
                callback();
            },
            fail: (err) => {
                uni.hideLoading();
                uni.showToast({
                    title: '保存失败，错误原因：' + err.errMsg,
                    icon: "none"
                });
            }
        });
        // #endif
    } else {
        uni.showToast({
            title: "url参数未设置",
            icon: "none"
        })
    }
};

// 下载文件
export function downloadFile(url, callback = () => {}) {
    uni.showLoading({
        title: '正在下载...'
    });
    uni.downloadFile({
        url,
        success: data => {
            if (data.statusCode == 200) {
                uni.hideLoading();
                callback(data.tempFilePath);
            } else {
                uni.hideLoading();
                uni.showToast({
                    title: '下载失败',
                    icon: "none"
                });
            }
        },
        fail: (err) => {
            uni.hideLoading();
            uni.showToast({
                title: '下载失败,错误原因：' + err.errMsg,
                icon: "none"
            });
        }
    });
};
